﻿/**
 * @file passwordlineedit.cpp
 * @author huangxb
 * @email 18228033207@163.com
 * @date 2024.04.13
 * @license MIT License
 * @brief 自定义密码输入框.
 *
 * @htmlonly
 * <span style="font-weight: bold">History</span>
 * @endhtmlonly
 * Version|Auther|Date|Describe
 * ------|----|------|--------
 * V1.0|huangxb|2024.04.13|创建
 */

/* 自定义头文件 */
#include "passwordlineedit.h"
#include "ui_passwordlineedit.h"
/* 库头文件 */
#include <QStyle>

/** @brief 控件样式. */
static const char* FMT_STYLESHEET = R"(
#m_inputVisible_pushButton[btnType="inputVisible"]{background:transparent;border:none;image:url(%1);}
#m_inputVisible_pushButton[btnType="inputInvisible"]{background:transparent;border:none;image:url(%2);}
#CPasswordLineEdit,#inputBoxWidget, #m_input_lineEdit{background:%3;}
#inputBoxWidget{border: 1px solid %4;}
#m_input_lineEdit{color: %5;font-size: %6;font-weight: %7;lineedit-password-character: %8;}
)";

CPasswordLineEdit::CPasswordLineEdit(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CPasswordLineEdit)
{
    ui->setupUi(this);

    /* 初始化控件样式 */
    InitDefaultStyleParam(m_styleParam);
    ui->m_inputVisible_pushButton->setProperty("btnType","inputInvisible");
    ui->m_inputStateIcon_label->setPixmap(m_styleParam.strInputStateIconImage);
    ui->m_inputStateIcon_label->setVisible(false);
    auto strStyle = GetStyleSheetByStyleParam(m_styleParam);
    setStyleSheet(strStyle);
    /* 初始化信号槽 */
    connect(ui->m_input_lineEdit, &QLineEdit::textChanged, this, &CPasswordLineEdit::textChanged);
    connect(ui->m_input_lineEdit, &QLineEdit::returnPressed, this, &CPasswordLineEdit::returnPressed);
    connect(ui->m_input_lineEdit, &QLineEdit::editingFinished, this, &CPasswordLineEdit::editingFinished);
}

CPasswordLineEdit::~CPasswordLineEdit()
{
    delete ui;
}

void CPasswordLineEdit::SetStyleParam(const StyleParam &styleParam)
{
    if(&m_styleParam == &styleParam)
    {
        return;
    }

    m_styleParam = styleParam;

    auto strStyle = GetStyleSheetByStyleParam(m_styleParam);
    setStyleSheet(strStyle);
}

const CPasswordLineEdit::StyleParam &CPasswordLineEdit::GetStyleParam() const
{
    return m_styleParam;
}

CPasswordLineEdit::StyleParam CPasswordLineEdit::GetDefaultStyleParam() const
{
    StyleParam param;
    InitDefaultStyleParam(param);
    return param;
}

void CPasswordLineEdit::ShowInputStateIcon(bool bShow)
{
    ui->m_inputStateIcon_label->setVisible(bShow);
}

void CPasswordLineEdit::setMaxLength(int nLen)
{
    ui->m_input_lineEdit->setMaxLength(nLen);
}

void CPasswordLineEdit::setText(const QString &arg)
{
    ui->m_input_lineEdit->setText(arg);
}

QString CPasswordLineEdit::text() const
{
    return ui->m_input_lineEdit->text();
}

void CPasswordLineEdit::InitDefaultStyleParam(StyleParam &styleParam) const
{
    /* 初始化默认样式参数 */
    styleParam.strInputVisibleImage = ":/resources/image/password_invisible.svg";
    styleParam.strInputInvisibleImage = ":/resources/image/password_visible.svg";
    styleParam.clrBackground = Qt::white;
    styleParam.clrBorder = QColor(0xe2,0xe2,0xe2);
    styleParam.clrInputText = QColor(0x86,0x90,0x9c);
    styleParam.fontInputText.setPixelSize(14);
    styleParam.fontInputText.setWeight(QFont::Normal);
    styleParam.strInputStateIconImage = ":/resources/image/input_correct.svg";
}

QString CPasswordLineEdit::GetStyleSheetByStyleParam(const StyleParam &styleParam) const
{
    QString strFmt(FMT_STYLESHEET);
    QString strStyleSheet = strFmt.arg(styleParam.strInputInvisibleImage, styleParam.strInputVisibleImage,
               styleParam.clrBackground.name(), styleParam.clrBorder.name(), styleParam.clrInputText.name(),
               QString::number(styleParam.fontInputText.pixelSize()), QString::number(styleParam.fontInputText.weight()),
               QString::number(styleParam.nPasswordChar));
    return strStyleSheet;
}

void CPasswordLineEdit::on_m_inputVisible_pushButton_clicked()
{
    auto strBtnType = ui->m_inputVisible_pushButton->property("btnType").toString();
    if(strBtnType == "inputInvisible")
    {
        ui->m_inputVisible_pushButton->setProperty("btnType","inputVisible");
        ui->m_input_lineEdit->setEchoMode(QLineEdit::Normal);
    }
    else if(strBtnType == "inputVisible")
    {
        ui->m_inputVisible_pushButton->setProperty("btnType","inputInvisible");
        ui->m_input_lineEdit->setEchoMode(QLineEdit::Password);
    }

    style()->polish(ui->m_inputVisible_pushButton);
}

